package com.shawn.mobliesafe;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.shawn.ui.SettingView;

public class SetUp2Activity extends SetUpBaseActivity {

    private SettingView sv_setup2_sim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_up2);
        sv_setup2_sim = (SettingView)findViewById(R.id.sv_setup2_sim);

        //设置回显操作
        //根据保存的SIM卡去初始化控件状态,有保存SIM卡号就是绑定SIM卡,如果没有就是没有绑定SIM卡
        if(TextUtils.isEmpty(sp.getString("sim",""))) {
            sv_setup2_sim.setChecked(false);
        }else {
            sv_setup2_sim.setChecked(true);
        }
        
        sv_setup2_sim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = sp.edit();
                if(sv_setup2_sim.isChecked()) {
                    editor.putString("sim","");
                    sv_setup2_sim.setChecked(false);
                }else {
                    //绑定SIM卡
                    //获取SIM卡号
                    //电话的管理者
                    TelephonyManager tel = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
                    String sim = tel.getSimSerialNumber();//获取SIM卡序列号,唯一标示
                    //保存SIM卡号
                    editor.putString("sim",sim);
                    sv_setup2_sim.setChecked(true);
                }
                editor.commit();
            }
        });
    }

    @Override
    public void next_activity() {
        //判断用户是否绑定SIM卡,绑定跳转到第三个界面,没有绑定,提醒用户必须绑定
        if(sv_setup2_sim.isChecked()) {
            Intent intent = new Intent(this,SetUp3Activity.class);
            startActivity(intent);
            finish();
            overridePendingTransition(R.anim.setup_enter_next, R.anim.setup_exit_next);
        }else {
            Toast.makeText(getApplicationContext(), "绑定SIM卡,才能进行后续设置", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void pre_activity() {
        Intent intent = new Intent(this,SetUp1Activity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.setup_enter_pre, R.anim.setup_exit_pre);
    }
}
